<?php

namespace backend\services\system;

use backend\extensions\ApiBaseService;
use backend\models\system\Role;

/**
 * Desc 角色服务类
 * @author HUI
 */
class RoleService extends ApiBaseService {
    
    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params){
        //检索条件
        $condition = ['AND'];
        if($params['name']){
            $condition[] = ['like','name',$params['name']];
        }
        $list = Role::getDataAll($condition,$params['page'],$params['page_size']);
        foreach ($list['list'] as &$value) {
            $value['status_name'] = Role::$status[$value['status']];
        }
        return $list;
    }
    
    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function create($params){
        if(Role::getDataOne(['name'=>$params['name']])){
            return self::jsonError('账号已存在');
        }
        if(Role::create($params) == false){
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess();
    }
    
    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function update($params){
        $condition = ['id'=>$params['id']];
        unset($params['id']);
        if(Role::updateData($params,$condition) === false){
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }
    
    /**
     * 删除
     * @param array $params 参数 
     * **/
    public static function delete($params){
        $condition = ['id'=>$params['id']];
        if(Role::del($condition) == false){
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }
}
